package com.suning.sawp.service.listener;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.suning.rsc.dto.responsedto.MbfServiceResponse;
import com.suning.rsc.mqservice.ei.annotation.EsbEIServerService;
import com.suning.rsc.server.NullResponseDto;
import com.suning.rsc.server.mq.MQServerServiceHandler;
import com.suning.sawp.constants.LoggerConstants;
import com.suning.sawp.dto.common.NsfNullReponseInterfaceForEsb;
import com.suning.sawp.dto.esb.ServiceNumMsgReq;
import com.suning.vgs.follow.remote.intf.ServiceNumRemoteService;

/**
 * 
 * 发送服务号消息监听类<br> 
 * 〈功能详细描述〉
 * SnsawpMangement-sendNoticeMsg
 * @author Wuxiao
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@EsbEIServerService(connectionFactory = "snsawpConnectionFactory", receiveQueue = "serviceNumSendQueue")
public class ServiceNumSendMessageListener extends MQServerServiceHandler<ServiceNumMsgReq, NullResponseDto, NsfNullReponseInterfaceForEsb>{

    private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConstants.SAWP_INTEGRATION);
    
    @Resource
    ServiceNumRemoteService serviceNumRemoteService;
    
//    @Resource
//    HsRemoteService hsRemoteService;
    
    private static final String SEPARATOR = ",";
    /**
     * 店+发送IM消息key
     */
    private static final String MSG_KEY = "ebuy_remote_msg";
    @Override
    public NullResponseDto handleBizz(ServiceNumMsgReq req, String uid) {
        LOGGER.debug("ServiceNumSendMessageListener接收数据：uid:{}-"+req.getServiceNumId()+"-"+req.getSysCode()+"-"+req.getContent(), uid);
        if(!checkStr(req.getServiceNumId(),req.getSysCode(),req.getContent()) || CollectionUtils.isEmpty(req.getStaffIdList())){
            LOGGER.info("店+服务号消息推送入参不合法:"+req.getServiceNumId()+"-"+req.getSysCode()+"-"+req.getContent());
            return null;
        }
        
        /*
         * 老IM功能关闭，暂时搞不起来了
        ServiceNumDto dto = serviceNumRemoteService.queryServiceNumByNumId(req.getServiceNumId());
        if(dto != null){
            List<String> systemIdList = dto.getSystemIdList();
            if(CollectionUtils.isNotEmpty(systemIdList)){
                boolean flag = false;
                //判断来源系统是否有权限使用该服务号
                for(String s : systemIdList){
                    if(req.getSysCode().equals(s)){
                        flag = true;
                        break;
                    }
                }
                if(flag){
                    //查询服务号队形的云导购Id
                    String from = serviceNumRemoteService.queryCloudGuideIdByStaffId(dto.getServiceNumId());
                    String nickName = dto.getServiceNumName();
                    List<Map<String,Object>> messageList = new ArrayList<Map<String,Object>>();
                    Map<String,Object> msgMap = new HashMap<String,Object>();
                    msgMap.put("from", from);
                    StringBuilder sb = new StringBuilder();
                    for(StaffInfo staff : req.getStaffIdList()){
                        String staffGuidId = serviceNumRemoteService.queryCloudGuideIdByStaffId(staff.getStaffId());
                        if(StringUtils.isNotBlank(staffGuidId)){
                            sb.append(staffGuidId).append(SEPARATOR);
                        }
                    }
                    String to = sb.toString();
                    if(to.length() > 0){
                        to = to.substring(0, to.length() - 1);
                    }
                    msgMap.put("to", to); 
                    msgMap.put("genre", "1");
                    msgMap.put("sender", nickName);
                    msgMap.put("content", req.getContent());
                    messageList.add(msgMap);
                    String data = GsonUtils.toJson(messageList);
                    LOGGER.debug("店+服务号发送消息请求:{}",data);
                    hsRemoteService.process(MSG_KEY, data);
                }
            }
        }
        */
        
        return null;
    }

    private boolean checkStr(String...strs){
        for(String str:strs){
            if(StringUtils.isBlank(str)){
                return false;
            }
        }
        return true;
    }
    @Override
    public void sendResponse(NsfNullReponseInterfaceForEsb clientInterface,
            NullResponseDto response, MbfServiceResponse serviceResponse) {
        LOGGER.debug("no response");
    }

}
